package utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 数据库连接工具类
 *
 */
public class SqlConnection {
	private static String driver;
	private static String url;
	private static String user;
	private static String pass;
	
	
	//静态代码块
	static {
		init();
	}
	// 1. 初始化
	private static void init() {
		InputStream in = SqlConnection.class.getResourceAsStream("/mysqlUtil.properties");
		Properties pro = new Properties();
		try {
			pro.load(in);
			driver = pro.getProperty("driver");
			url = pro.getProperty("url");
			user = pro.getProperty("user");
			pass = pro.getProperty("pass");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("资源加载失败");
		}
		
	}
	// 2. 数据库连接类
	public static Connection getConnection() {
		Connection con = null;
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, user , pass);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接失败");
		}//用反射加载
		
		return con;
	}
	// 3. 查询类
	public static ResultSet executeQuery(Connection con,PreparedStatement pstm,ResultSet res,String sql,Object[] params) {
	
		try {
			pstm = con.prepareStatement(sql);
			int i = 1;
			for(Object obj : params) {
				pstm.setObject(i, obj);
				i++;
			}
			res = pstm.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
	
	// 4. 更新操作
	public static int executeUpdate(Connection con,PreparedStatement pstm,String sql,Object[] params) {
		int row = 0;
		try {
			pstm = con.prepareStatement(sql);
			int i = 1;
			for(Object obj : params) {
				pstm.setObject(i, obj);
				i++;
			}
			row = pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return row;
	}
	
	// 5.关闭资源
    public static boolean closeConnection(Connection connection,PreparedStatement pstm,ResultSet res) {
        boolean flag=true;//是否修改成功
        if (res!=null){
            try {
                res.close();
                res=null;
            } catch (SQLException e) {
                flag=false;
                e.printStackTrace();
            }

        }
        if (pstm!=null){
            try {
                pstm.close();
                pstm=null;
            } catch (SQLException e) {
                flag=false;
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
                connection=null;
            } catch (SQLException e) {
                flag=false;
                e.printStackTrace();
            }
        }

        return flag;

    }
	
	
	
	public static void main(String[] args) {
//		Connection con = SqlConnection.getConnection();
//		PreparedStatement pstm = null;
//		ResultSet res = null;
//		Object[] params = {};
//		String sql = "select * from user";
//		res = SqlConnection.executeQuery(con, pstm, res, sql, params);
//		
//		try {
//			while(res.next()) {
//					System.out.println(res.getObject(2));
//			}
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}
